/*
StatelessWidget框架,
使用MaterialApp框架,需在app的main()中,使用runXAppInMain()替代原来的runApp();
注意:
即是内部多个child都是StatefulWidget,
如果它们有关联联动,其中一个child的setState方法只能通知自己更新,很难通知其他Stateful的child更新,
因此,如果有多个联动的child,则建议其所有child的根Widget开始,就使用stateful,
联动方法也在其总的stateful那层,那么通知更新时,就会重新执行build
 */

//StatelessWidget/////////////////////////////////////////////////////////
/*
 StatelessWidget父类基础界面
 */

import 'package:flutter/material.dart';
import 'package:xsdk_flutter/xsdk/framework/base/xsdk_function.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_color.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_dart.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_flutter.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_log.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_toast.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_widget.dart';

abstract class XStatelessWidget extends StatelessWidget {

  dynamic xData;

  XStatelessWidget({this.xData,super.key});

  late BuildContext xContext;

  @override
  Widget build(BuildContext context) {
    xContext=context;
    //
    onStart();
    //
    return getBuildWidget();
  }

  //初始化
  @required
  void onStart();

  //主内容body ContentView
  @required
  Widget getBuildWidget();

}

abstract class XPageStatelessWidget extends XStatelessWidget {

  String? xTag;
  //适用处理业务后再关闭当前页面,对应的处理方法在上一界面传入)
  XFunctionPageStateFinish? handleAndFinishPageFunction;

  XPageStatelessWidget({
    this.xTag,
    super.xData,
    this.handleAndFinishPageFunction,
  }):super(key:UniqueKey());

  @override
  String toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) {
    return XDartUtils.getRuntimeTypeName(this);
  }

  @override
  String toStringShort() {
    return toString();
  }



  //Loading////////////////////////////////////////////////////
  /*
  更多: https://pub-web.flutter-io.cn/packages/loading_animation_widget
   */

  OverlayEntry? _loadingOverlayEntry;

  void showLoading({Color? indicatorColor}){

    try {

      if(_loadingOverlayEntry!=null){
        return;
      }

      _loadingOverlayEntry = OverlayEntry(
        builder: (_) {
          return IgnorePointer(
            child: XWidgetUtils.getLayoutColumn(
                mainAxisAlignment:MainAxisAlignment.center,
                children: [
                  XWidgetUtils.getLayoutCenter(
                      SizedBox(
                          width: 20,height: 20,
                          child:CircularProgressIndicator(
                            strokeWidth: 2.0,
                            color: indicatorColor??XColorUtils.appMainColor,
                            // backgroundColor: Colors.blue,
                            // valueColor: AlwaysStoppedAnimation<Color>(Colors.red),
                          )
                      )
                  )
                ]
            ),
          );
        },
      );


      Overlay.of(xContext).insert(_loadingOverlayEntry!);

    }catch (e) {
      XLogUtils.printLog('---showLoading报错:${e.toString()}');
    }


  }
  void hideLoading(){
    try {
      _loadingOverlayEntry?.remove();
      _loadingOverlayEntry = null;
    }catch(e){
      //
    }
  }


  void toast(String msg){
    XToastUtils.toast(context: xContext, msg: msg);
  }

  /////////////////////


  void finishPage(
      {
        dynamic result,
        String? backToPointPageName,//指定返回界面:   返回首页'/'
      }
      ) {

    //蒙层动画正方向运行
    //_animationControllerMengcheng?.forward();
    //蒙层动画反方向运行
    //_animationControllerMengcheng?.reverse();

    //
    XFlutterUtils.finishPage(xContext,result: result, backToPointPageName: backToPointPageName);
  }


  //处理业务后再关闭(执行上一页面传入的处理方法)
  void finishAfterHandleFunction(dynamic result){
    if(handleAndFinishPageFunction!=null){
      handleAndFinishPageFunction!(result,finishPage);
    }else{
      finishPage(result:result);
    }
  }





}


class XDefaultStatelessWidget extends XStatelessWidget {

  @override
  void onStart() {
  }

  @override
  Widget getBuildWidget() {
    return const Text('');
  }

}